﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MahApps.Metro.Controls;
using System.Data;

namespace MauThuTabControl
{
    /// <summary>
    /// Interaction logic for CTXuatHang.xaml
    /// </summary>
    public partial class CTXuatHang : MetroWindow
    {
        public CTXuatHang()
        {
            InitializeComponent();
        }

        ConnectionDB cn = new ConnectionDB();
        public int _CoThaoTac = 0;
        public string _KhoaChinh = "";
       
        
        
        
       void EnalBuuton(bool b1,bool b2 ) 
        {
            _btGo.IsEnabled = b2;
            _btBack.IsEnabled = b2;
            _btuu.IsEnabled = b1;
            _bthuy.IsEnabled = b1;
            
            _grTTPhieuN.IsEnabled = b1;

        }
        void LoadCB()
        {
            DataTable dt = cn.LoadData("select * from DMKhachHang");
           


            _cbNCC.DisplayMemberPath = "tenKh";
            _cbNCC.SelectedValuePath = "maKh";
            _cbNCC.ItemsSource = dt.DefaultView;
            _cbNCC.SelectedIndex = 0;

         
        }
        private string _ChuyenNgayMMddyyyy(string Ngay)
        {
            return Ngay.Substring(3, 2) + "/" + Ngay.Substring(0, 2) + "/" + Ngay.Substring(6, 4);

        }
         void LoadDMSP()
        {
            DataTable dt = cn.Loadtree("  SELECT dbo.DMSanPham.maSanPham as maSanPham1, dbo.DMSanPham.tenSanPham as tenSanPham1, dbo.DMSanPham.giaBan as giaBan1, dbo.DMNhomSP.tenNhomSP FROM dbo.DMNhomSP INNER JOIN dbo.DMSanPham ON dbo.DMNhomSP.maNhomSP = dbo.DMSanPham.maNhomSP where  dbo.DMSanPham.maSanPham NOT IN (select dbo.QLXuatHangCT.maSanPham from dbo.QLXuatHangCT where dbo.QLXuatHangCT.soPhieuX=N'"+_txtSoPhieuX.Text+"')");
            _dtDMSP.ItemsSource = dt.DefaultView;
            ListCollectionView collection = new ListCollectionView(dt.DefaultView);
           
            collection.GroupDescriptions.Add(new PropertyGroupDescription("tenNhomSP"));
            _dtDMSP.ItemsSource = collection;
            
            
          

        }
         void LoadCTSP() 
        {
            DataTable dt = cn.Loadtree("  SELECT dbo.QLXuatHangCT.maSanPham, dbo.DMSanPham.tenSanPham, dbo.QLXuatHangCT.soLuong, dbo.QLXuatHangCT.donGia, dbo.QLXuatHangCT.thanhTien FROM dbo.DMSanPham INNER JOIN dbo.QLXuatHangCT ON dbo.DMSanPham.maSanPham = dbo.QLXuatHangCT.maSanPham where QLXuatHangCT.soPhieuX=N'" + _txtSoPhieuX.Text + "'");
            _dtCTNhap.ItemsSource = dt.DefaultView;
            
          
        }
        private void _NUTongTien_GotFocus(object sender, RoutedEventArgs e)
        {
            _NUTongTien.HideUpDownButtons = false;
        }

        private void _NUTongTien_LostFocus(object sender, RoutedEventArgs e)
        {
            _NUTongTien.HideUpDownButtons = true;
            
        }

        private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
        {
            LoadCB();
            if (_CoThaoTac == 1)
            {

            }
           
            if (_CoThaoTac == 2) 
            {
                DataTable dt = cn.Loadtree("SELECT     dbo.QLXuatHang.soPhieuX, dbo.QLXuatHang.ngayXuat, dbo.DMKhachHang.tenKh, dbo.QLXuatHang.ghiChu, dbo.QLXuatHang.tongTien FROM dbo.QLXuatHang INNER JOIN dbo.DMKhachHang ON dbo.QLXuatHang.maKh = dbo.DMKhachHang.maKh where QLXuatHang.soPhieuX=N'" + _KhoaChinh + "'");
                _dtDMSP.ItemsSource = dt.DefaultView;
                if (dt.Rows.Count> 0)
                {
                    _txtSoPhieuX.IsReadOnly = true;
                    _txtSoPhieuX.Text = dt.Rows[0]["soPhieuX"].ToString().Trim();
                    _txtNgayLap.Text = dt.Rows[0]["ngayXuat"].ToString().Trim();
                    _cbNCC.Text = dt.Rows[0]["tenKh"].ToString().Trim();
                    _NUTongTien.Value = Convert.ToDouble(dt.Rows[0]["tongTien"].ToString().Trim());
                    _txtGhiChu.Text = dt.Rows[0]["ghiChu"].ToString().Trim();
                }
                
            }
            LoadCTSP();
            LoadDMSP();
            
        }

        private void _btBack_Click(object sender, RoutedEventArgs e)
        {
            if (_dtCTNhap.SelectedIndex > -1)
            {
                cn._ThucThiCauTruyVan("delete from QLXuatHangCT where soPhieuX=N'" + _txtSoPhieuX.Text + "' and " +
                    "maSanPham=N'" + ((DataRowView)_dtCTNhap.SelectedItem).Row[0].ToString() + "'");
                LoadCTSP();
                LoadDMSP();
            }
        }

        private void _btGo_Click(object sender, RoutedEventArgs e)
        {
            if (_dtDMSP.SelectedIndex > -1)
            {
                cn._ThucThiCauTruyVan("insert into QLXuatHangCT(soPhieuX,maSanPham,soLuong,donGia,thanhTien) values(N'" + _txtSoPhieuX.Text + "', " +
                 "N'" + ((DataRowView)_dtDMSP.SelectedItem).Row[0].ToString() + "','0','" + ((DataRowView)_dtDMSP.SelectedItem).Row[2].ToString() + "','0')");
                LoadCTSP();
                LoadDMSP();
            }
        }

        private void _bthuy_Click(object sender, RoutedEventArgs e)
        {

            this.Close();
        }

        private void _btuu_Click(object sender, RoutedEventArgs e)
        {
           
            if (KiemTraLuu()==true)
            {


                bool _KiemTra = false;
                if (_CoThaoTac == 1)
                {
                    if (_txtSoPhieuX.Text.TrimEnd() == cn._LayTruongKieuChuoi("select soPhieuX from QLXuatHangCT where soPhieuX='" + _txtSoPhieuX.Text + "'"))
                    {
                        MessageBox.Show("<Số Phiếu> Này đã tồn tại!!!", "Thông báo");
                        _txtSoPhieuX.Focus();
                    }
                    else
                    {
                        float __tongTien = float.Parse(_NUTongTien.Value.ToString().Replace(",", ""));
                        
                        DateTime _ngayLap = DateTime.Parse(_txtNgayLap.Text);
                        string _Queryinsert = "insert into QLXuatHang(soPhieuX,ngayXuat,maKh,ghiChu,tongTien) " +
                            "values(N'" + _txtSoPhieuX.Text + "',N'" + _ChuyenNgayMMddyyyy(_txtNgayLap.Text) + "', " +
                            "N'" + _cbNCC.SelectedValue.ToString() + "', " +
                            "N'" + _txtGhiChu.Text + "', " +
                            "'" + _NUTongTien.Value.ToString().Replace(",", "") + "')";
                        _KiemTra = cn._ThucThiCauTruyVan(_Queryinsert);
                    }
                  

                }
                else
                {
                    float __tongTien = float.Parse(_NUTongTien.Value.ToString().Replace(",", "."));
                    DateTime _ngayLap = DateTime.Parse(_txtNgayLap.Text);
                    string _Queryupdate = "update QLXuatHang set ngayXuat=N'" + _ChuyenNgayMMddyyyy(_txtNgayLap.Text) + "', " +
                    "maKh=N'" + _cbNCC.SelectedValue.ToString() + "',ghiChu=N'"+_txtGhiChu.Text+"' ," +
                    "tongTien='" + _NUTongTien.Value.ToString().Replace(",", "") + "' " +
                    "where soPhieuX=N'" + _txtSoPhieuX.Text + "'";
                    _KiemTra = cn._ThucThiCauTruyVan(_Queryupdate);
                }
                if (_KiemTra == true)
                {
                    _btGo.IsEnabled = true;
                    _btBack.IsEnabled = true;
                    _bthuy.IsEnabled = false;
                    _btuu.IsEnabled = false;
                    _grTTPhieuN.IsEnabled = false;
                }
            }
           
        }

        public bool KiemTraLuu() 
        {
            if (_txtSoPhieuX.Text.TrimEnd() == "") 
            {
                MessageBox.Show("<SỐ PHIẾU> Không được bỏ trống!!!","Chú ý");
                _txtSoPhieuX.Focus();
                return false;
            }
            return true;
        }

        private void _btTimKiem_Click(object sender, RoutedEventArgs e)
        {
            if (_rdMaSP.IsChecked == true)
            {
                if (_txtTimKiem.Text != "")
                {
                    DataTable dt = cn.Loadtree("SELECT dbo.DMSanPham.maSanPham as maSanPham1, dbo.DMSanPham.tenSanPham as tenSanPham1, dbo.DMNhomSP.tenNhomSP FROM dbo.DMNhomSP INNER JOIN dbo.DMSanPham ON dbo.DMNhomSP.maNhomSP = dbo.DMSanPham.maNhomSP where dbo.DMSanPham.maSanPham='" + _txtTimKiem.Text + "'");
                    _dtDMSP.ItemsSource = dt.DefaultView;
                    _dtDMSP.UnselectAll();
                    ListCollectionView collection = new ListCollectionView(dt.DefaultView);
                    collection.GroupDescriptions.Add(new PropertyGroupDescription("tenNhomSP"));
                    _dtDMSP.ItemsSource = collection;

                }
                else
                {
                    MessageBox.Show("Nhập thông tin tìm kiếm", "Chú ý");
                }
            }
            else
            {
                if (_txtTimKiem.Text != "")
                {
                   
                        DataTable dt = cn.Loadtree("SELECT dbo.QLXuatHangCT.soPhieuX, dbo.QLXuatHangCT.maSanPham, dbo.DMSanPham.tenSanPham, dbo.QLXuatHangCT.soLuong, dbo.QLXuatHangCT.donGia, dbo.QLXuatHangCT.thanhTien FROM dbo.DMSanPham INNER JOIN dbo.QLXuatHangCT ON dbo.DMSanPham.maSanPham = dbo.QLXuatHangCT.maSanPham where dbo.DMSanPham.tenSanPham like N'%" + _txtTimKiem.Text + "%'");
                        _dtDMSP.ItemsSource = dt.DefaultView;
                        _dtDMSP.UnselectAll();
                        ListCollectionView collection = new ListCollectionView(dt.DefaultView);
                        collection.GroupDescriptions.Add(new PropertyGroupDescription("tenNhomSP"));
                 


                }
                else
                {
                    MessageBox.Show("Nhập thông tin tìm kiếm", "Chú ý");
                }
            }
        }
    }
    }

